package com.example.fish.controller;

import com.example.fish.MessageTestBox;
import com.example.fish.pojo.User;
import com.example.fish.service.UserService;
import com.example.fish.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/user")
public class UserController extends BaseController {

    @Autowired
    UserService userService;
    /***
     * 注册功能
     * @return
     */
    @PostMapping("/register")
    public JsonResult<Integer> register(User user){
        Integer integer = userService.insertUser(user);
        return new JsonResult<>(200,"success",integer);
    }

    @PostMapping("/login")
    public JsonResult<User> getUserById(String username,String password, HttpSession session){
        User loginUser = userService.login(username, password);
        session.setAttribute("user",loginUser);
        return new JsonResult<User>(200,"login success",loginUser);
    }

//    退出登录
    @GetMapping("/logout")
    public JsonResult<Void> logout(HttpSession session){
        session.removeAttribute("user");
        return new JsonResult<>(200,"logout success",null);
    }

    @PostMapping("/updatePassword")
    public JsonResult<Integer> updatePassword(@RequestParam("oldpassword") String oldPassword,@RequestParam("newpassword") String newPassword, HttpSession session){
        User user =(User) session.getAttribute("user");
        Integer integer = userService.updatePassword(oldPassword, newPassword, user);
        return new JsonResult<Integer>(200,"success",integer);
    }

    @GetMapping("/haha")
    public MessageTestBox<String> haha(){
        return new MessageTestBox<>(200,"success","haha");
    }
    @GetMapping("/hehe")
    public JsonResult<String> hehe(){
        return new JsonResult<>(200,"success","hehe");
    }
}
